package cn.xhy.material.controller.admin.pojectinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.xhy.material.controller.admin.pojectinfo.vo.PojectInfoPageReqVO;
import cn.xhy.material.controller.admin.pojectinfo.vo.PojectInfoRespVO;
import cn.xhy.material.controller.admin.pojectinfo.vo.PojectInfoSaveReqVO;
import cn.xhy.material.dal.dataobject.pojectinfo.ContractDO;
import cn.xhy.material.dal.dataobject.pojectinfo.CustomerProjectDO;
import cn.xhy.material.dal.dataobject.pojectinfo.PojectInfoDO;
import cn.xhy.material.service.pojectinfo.PojectInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 项目信息")
@RestController
@RequestMapping("/material/poject-info")
@Validated
public class PojectInfoController {

    @Resource
    private PojectInfoService pojectInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建项目信息")
    @PreAuthorize("@ss.hasPermission('material:poject-info:create')")
    public CommonResult<Long> createPojectInfo(@Valid @RequestBody PojectInfoSaveReqVO createReqVO) {
        return success(pojectInfoService.createPojectInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目信息")
    @PreAuthorize("@ss.hasPermission('material:poject-info:update')")
    public CommonResult<Boolean> updatePojectInfo(@Valid @RequestBody PojectInfoSaveReqVO updateReqVO) {
        pojectInfoService.updatePojectInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('material:poject-info:delete')")
    public CommonResult<Boolean> deletePojectInfo(@RequestParam("id") Long id) {
        pojectInfoService.deletePojectInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('material:poject-info:query')")
    public CommonResult<PojectInfoRespVO> getPojectInfo(@RequestParam("id") Long id) {
        PojectInfoDO pojectInfo = pojectInfoService.getPojectInfo(id);
        return success(BeanUtils.toBean(pojectInfo, PojectInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目信息分页")
    @PreAuthorize("@ss.hasPermission('material:poject-info:query')")
    public CommonResult<PageResult<PojectInfoRespVO>> getPojectInfoPage(@Valid PojectInfoPageReqVO pageReqVO) {
        PageResult<PojectInfoDO> pageResult = pojectInfoService.getPojectInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PojectInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目信息 Excel")
    @PreAuthorize("@ss.hasPermission('material:poject-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPojectInfoExcel(@Valid PojectInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PojectInfoDO> list = pojectInfoService.getPojectInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目信息.xls", "数据", PojectInfoRespVO.class,
                        BeanUtils.toBean(list, PojectInfoRespVO.class));
    }

    // ==================== 子表（销售合同） ====================

    @GetMapping("/contract/list-by-project-info-id")
    @Operation(summary = "获得销售合同列表")
    @Parameter(name = "projectInfoId", description = "项目信息id标识")
    @PreAuthorize("@ss.hasPermission('material:poject-info:query')")
    public CommonResult<List<ContractDO>> getContractListByProjectInfoId(@RequestParam("projectInfoId") Integer projectInfoId) {
        return success(pojectInfoService.getContractListByProjectInfoId(projectInfoId));
    }

    // ==================== 子表（客户和项目关系） ====================

    @GetMapping("/customer-project/list-by-project-id")
    @Operation(summary = "获得客户和项目关系列表")
    @Parameter(name = "projectId", description = "项目ID")
    @PreAuthorize("@ss.hasPermission('material:poject-info:query')")
    public CommonResult<List<CustomerProjectDO>> getCustomerProjectListByProjectId(@RequestParam("projectId") String projectId) {
        return success(pojectInfoService.getCustomerProjectListByProjectId(projectId));
    }

}
